package com.excuse.excusebuilder.view.fragment;

import android.support.design.widget.FloatingActionButton;
import android.view.View;
import android.widget.ExpandableListView;

import com.excuse.excusebuilder.R;
import com.excuse.excusebuilder.adapter.HomeAdapter;
import com.excuse.excusebuilder.base.BaseFragment;
import com.excuse.excusebuilder.bean.Option;
import com.excuse.excusebuilder.bean.Type;
import com.excuse.excusebuilder.presenter.HomeFragmentPresenter;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import butterknife.Unbinder;


/**
 * Created by U-nookia on 2017/4/13.
 * 首页
 */

public class HomeFragment extends BaseFragment implements HomeFragmentPresenter.IView {
    @BindView(R.id.add_excuse_fab)
    FloatingActionButton enterExcuseFab;
    private HomeAdapter adapter;
    private HomeFragmentPresenter presenter;
    @BindView(R.id.expandable_list)
    ExpandableListView expandableList;

    @Override
    protected void initData() {
        presenter = new HomeFragmentPresenter(getContext(), this);
        expandableList.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
                Option option = (Option) adapter.getChild(groupPosition, childPosition);
                presenter.enterExcuseActivity(option);
                return true;
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        presenter.getList();
    }

    @Override
    public int getFragmentLayout() {
        return R.layout.fragment_home;
    }


    @Override
    public void setList(List<Type> types, List<List<Option>> dataList) {
        adapter = new HomeAdapter(types, dataList, getContext());
        adapter.setListener(new HomeAdapter.OnGroupAddClickListener() {
            @Override
            public void onClick(Type type) {
                presenter.enterAddOptionActivity(type);
            }
        });
        expandableList.setAdapter(adapter);
    }


    @OnClick(R.id.add_excuse_fab)
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.add_excuse_fab:
                presenter.enterAddExcuseActivity();
                break;
        }
    }

    @Override
    public void showRefreshing(boolean isShow) {

    }

    @Override
    public void finish() {
        getActivity().finish();
    }
}
